define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'merchant_car/index' + location.search,
                    add_url: 'merchant_car/add?merchant_id='+ Fast.api.query('merchant_id'),
                    edit_url: 'merchant_car/edit?merchant_id='+ Fast.api.query('merchant_id'),
                    del_url: 'merchant_car/del',
                    multi_url: 'merchant_car/multi',
                    check_url: 'merchant_car/check',
                    table: 'merchant_car',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        // {field: 'merchant_car_name', title: __('Merchant_car_name'),operate: 'LIKE'},
                        {field: 'merchant.merchant_name', title: __('Merchant_name'), searchList: Config.merchantNameList, formatter: Table.api.formatter.label},
                        {field: 'merchant.merchant_phone', title: __('Merchant_Phone'), operate: 'like'},
                        {field: 'merchant.shop_name', title: __('Shop_name'), searchList: Config.shopNameList, formatter: Table.api.formatter.label},
                        {field: 'car.car_name', title: __('平台车辆'),operate: 'LIKE'},
                        {field: 'car.brand_id', title: __('Brand_id'), searchList: Config.brandList, formatter: Table.api.formatter.label},
                        {field: 'car.type_id', title: __('Type_id'), searchList: Config.typeList, formatter: Table.api.formatter.label},
                        {field: 'car.car_site', title: __('座位数')},
                        {field: 'car.car_gate', title: __('车门数')},
                        {field: 'car.model_id', title: __('Model_id'), searchList: Config.configureList, formatter: Table.api.formatter.label},
                        {field: 'car.car_gear', title: __('档位'), searchList: Config.gearlist},
                        {field: 'drive', title: __('Drive'), searchList: {'1': '两驱', '2': '四驱'}, formatter: Table.api.formatter.label},
                        {field: 'displacement', title: __('Displacement'), operate: 'like'},
                        {field: 'car_number', title: __('Car_number'), operate:'like'},
                        {field: 'car_price', title: __('Car_price'), operate:'RANGE'},
                        {field: 'handle_fee', title: __('Handle_fee'), operate:false},
                        {field: 'night_pick_fee', title: __('Night_pick_fee'), operate:false},
                        {field: 'night_return_fee', title: __('Night_return_fee'), operate:false},
                        // {field: 'car_deposit', title: __('Car_deposit'), operate:false},
                        {field: 'status', title: __('Status'), searchList: Config.statuslist, formatter: Table.api.formatter.toggle,yes:1,no:2},
                        {field: 'examine_status', title: __('Examine_status'), searchList: Config.checkStatuslist, formatter: Table.api.formatter.label},
                        {field: 'car_status', title: __('Car_status'), searchList: Config.carStatuslist, formatter: Table.api.formatter.label},
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate,
                            buttons: [{
                                name: 'far_price',
                                text: function (row) {
                                    return '远期价格(' + row.price_count + ')';
                                },
                                icon: 'fa fa-list',
                                classname: 'btn btn-info btn-xs btn-detail btn-dialog',
                                url: 'merchant_car/far_price'
                            }],
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);

        },
        add: function () {
            var brand_id = $("#c-brand_id").val();
            var type_id = $("#c-type_id").val();
            var model_id = $("#c-model_id").val();
            // selectpageCate(brand_id);
            // selectpageCar(brand_id, type_id, model_id);
            $(document).on("change", "#c-brand_id", function(){
                brand_id = $("#c-brand_id").val();
                selectpageCate(brand_id);
            });
            function selectpageCate(brand_id){
                $.post("car_cate/index",{
                    //搜索条件，上一个selectpage选择完后传过来的id作为此次搜索的条件
                    custom: {pid: brand_id, type:3},
                    keyField:'id',
                    showField:'cate_name',
                },function(result){
                    //刷新数据的关键，我也是看了文档才知道有这么一个方法，动态更改数据源
                    $('#c-type_id').selectPageData(result.list);
                });
            }

            $(document).on("change", "#c-model_id", function(){
                brand_id = $("#c-brand_id").val();
                type_id = $("#c-type_id").val();
                model_id = $("#c-model_id").val();
                selectpageCar(brand_id, type_id, model_id);
            });
            $(document).on("change", "#c-type_id", function(){
                brand_id = $("#c-brand_id").val();
                type_id = $("#c-type_id").val();
                model_id = $("#c-model_id").val();
                selectpageCar(brand_id, type_id, model_id);
            });

            function selectpageCar(brand_id, type_id, model_id){
                $.post("car/index",{
                    //搜索条件，上一个selectpage选择完后传过来的id作为此次搜索的条件
                    custom: {brand_id: brand_id, model_id:model_id, type_id:type_id},
                    keyField:'id',
                    showField:'car_name',
                },function(result){
                    //刷新数据的关键，我也是看了文档才知道有这么一个方法，动态更改数据源
                    $('#c-car_id').selectPageData(result.list);
                });
            }

            Template.helper("Fast", Fast);
            //因为日期选择框不会触发change事件，导致无法刷新textarea，所以加上判断
            $(document).on("dp.change", "#add-form .datetimepicker", function () {
                $(this).parent().prev().find("input").trigger("change");
            });
            $(document).on("fa.event.appendfieldlist", "#first-table .btn-append", function (e, obj) {
                // //绑定动态下拉组件
                // Form.events.selectpage(obj);
                //绑定日期组件
                Form.events.datetimepicker(obj);
                // //绑定上传组件
                // Form.events.faupload(obj);

                // //上传成功回调事件，变更按钮的背景
                // $(".upload-image", obj).data("upload-success", function (data) {
                //     $(this).css("background-image", "url('" + Fast.api.cdnurl(data.url) + "')");
                // })
            });
            $(document).on("fa.event.appendfieldlist", "#second-table .btn-append", function (e, obj) {
                //绑定日期组件
                Form.events.datetimepicker(obj);
            });
            // Form.api.bindevent($("form[role=form]"), function (data, ret) {
            //     // Layer.alert(data.data);
            // });
            Controller.api.bindevent();
        },
        edit: function () {
            var brand_id = $("#c-brand_id").val();
            var type_id = $("#c-type_id").val();
            var model_id = $("#c-model_id").val();
            // selectpageCate(brand_id);
            // selectpageCar(brand_id, type_id, model_id);
            $(document).on("change", "#c-brand_id", function(){
                brand_id = $("#c-brand_id").val();
                selectpageCate(brand_id);
            });
            function selectpageCate(brand_id){
                $.post("car_cate/index",{
                    //搜索条件，上一个selectpage选择完后传过来的id作为此次搜索的条件
                    custom: {pid: brand_id, type:3},
                    keyField:'id',
                    showField:'cate_name',
                },function(result){
                    //刷新数据的关键，我也是看了文档才知道有这么一个方法，动态更改数据源
                    $('#c-type_id').selectPageData(result.list);
                });
            }

            $(document).on("change", "#c-model_id", function(){
                brand_id = $("#c-brand_id").val();
                type_id = $("#c-type_id").val();
                model_id = $("#c-model_id").val();
                selectpageCar(brand_id, type_id, model_id);
            });
            $(document).on("change", "#c-type_id", function(){
                brand_id = $("#c-brand_id").val();
                type_id = $("#c-type_id").val();
                model_id = $("#c-model_id").val();
                selectpageCar(brand_id, type_id, model_id);
            });

            function selectpageCar(brand_id, type_id, model_id){
                $.post("car/index",{
                    //搜索条件，上一个selectpage选择完后传过来的id作为此次搜索的条件
                    custom: {brand_id: brand_id, model_id:model_id, type_id:type_id},
                    keyField:'id',
                    showField:'car_name',
                },function(result){
                    //刷新数据的关键，我也是看了文档才知道有这么一个方法，动态更改数据源
                    $('#c-car_id').selectPageData(result.list);
                });
            }

            Template.helper("Fast", Fast);
            //因为日期选择框不会触发change事件，导致无法刷新textarea，所以加上判断
            $(document).on("dp.change", "#add-form .datetimepicker", function () {
                $(this).parent().prev().find("input").trigger("change");
            });
            $(document).on("fa.event.appendfieldlist", "#first-table .btn-append", function (e, obj) {
                // //绑定动态下拉组件
                // Form.events.selectpage(obj);
                //绑定日期组件
                Form.events.datetimepicker(obj);
                // //绑定上传组件
                // Form.events.faupload(obj);

                // //上传成功回调事件，变更按钮的背景
                // $(".upload-image", obj).data("upload-success", function (data) {
                //     $(this).css("background-image", "url('" + Fast.api.cdnurl(data.url) + "')");
                // })
            });
            $(document).on("fa.event.appendfieldlist", "#second-table .btn-append", function (e, obj) {
                //绑定日期组件
                Form.events.datetimepicker(obj);
            });
            // Form.api.bindevent($("form[role=form]"), function (data, ret) {
            //     // Layer.alert(data.data);
            // });
            Controller.api.bindevent();
        },
        check: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});